package com.cskaoyan.service.adminservice.impl;


import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.goods.bo.UpdatePO;
import com.cskaoyan.bean.chaosbean.goods.vo.CatAndBrandVO;
import com.cskaoyan.bean.chaosbean.goods.vo.BrandVo;
import com.cskaoyan.bean.chaosbean.goods.vo.CategoryListDTO;
import com.cskaoyan.bean.chaosbean.goods.vo.Detail;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.adminservice.GoodService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author dengqwe
 * @datetime 2022/12/28 20:34
 * @description: TODO:
 */
@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketCartMapper cartMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;


    /**
     * @param basePageInfo page/limit/sort/order
     * @return
     */
    @Override
    public CommonData<MarketGoods> list(BasePageInfo basePageInfo, Integer goodsId, String goodsSn, String name) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketGoodsExample example = new MarketGoodsExample();
        //排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        MarketGoodsExample.Criteria criteria = example.createCriteria();
        //查询good表中的delete属性为0的商品
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (goodsId != null) {
            criteria.andIdEqualTo(goodsId);
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }


        List<MarketGoods> list = goodsMapper.selectByExample(example);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        int pageNum = pageInfo.getPageNum();//page
        int pageSize = pageInfo.getPageSize();//limit
        int pages = pageInfo.getPages();//pages
        long total = pageInfo.getTotal();//total


        List<MarketGoods> list1 = pageInfo.getList();
        CommonData<MarketGoods> data = new CommonData<>();
        data.setPage(pageNum);
        data.setLimit(pageSize);
        data.setPages(pages);
        data.setTotal((int) total);
        data.setList(list1);
        //封装成一个CommonData对象data
        return data;
    }

    @Override
    public void create(UpdatePO create) {

        //goodsMapper.insertSelective(create.getGoods());
        List<MarketGoodsProduct> products = create.getProducts();
        MarketGoodsProduct product = products.get(0);
        MarketGoods goods = create.getGoods();
        goods.setRetailPrice(product.getPrice());
        Date date = new Date();
        goods.setAddTime(date);
        goodsMapper.insertSelective(goods);


        //插入attributes id商品
        Integer id = goods.getId();
        List<MarketGoodsAttribute> attributes = create.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(id);
            attributeMapper.insertSelective(attribute);
        }
        //插入products
        for (MarketGoodsProduct product1 : products) {
            product1.setGoodsId(id);
            productMapper.insertSelective(product1);
        }

       // 插入getSpecifications
        List<MarketGoodsSpecification> specifications = create.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(id);
            specificationMapper.insertSelective(specification);
        }


    }

    @Override
    public CatAndBrandVO catAndBrand() {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria2 = example.createCriteria();
        List<MarketBrand> brands = brandMapper.selectByExample(example);
        //brandVoArrayList的一个list
        ArrayList<BrandVo> brandVoArrayList = new ArrayList<>();
        for (MarketBrand brand : brands) {
            BrandVo brandVo = new BrandVo();
            brandVo.setValue(brand.getId());
            brandVo.setLabel(brand.getName());
            brandVoArrayList.add(brandVo);
        }

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        List<MarketCategory> categoryList = categoryMapper.selectByExample(categoryExample);

        //category的一个list
        ArrayList<CategoryListDTO> list = new ArrayList<>();
        for (MarketCategory category : categoryList) {
            MarketCategoryExample categoryExample1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
            criteria1.andPidEqualTo(category.getId());
            List<MarketCategory> categoryList1 = categoryMapper.selectByExample(categoryExample1);
            CategoryListDTO categoryListDTO = new CategoryListDTO();

            LinkedList<CategoryListDTO.ChildrenDTO> childrenList = new LinkedList<>();
            //初始化value:label → List
            for (MarketCategory marketCategory : categoryList1) {
                CategoryListDTO.ChildrenDTO childrenDTO = new CategoryListDTO.ChildrenDTO();
                childrenDTO.setValue(marketCategory.getId());
                childrenDTO.setLabel(marketCategory.getName());
                childrenList.add(childrenDTO);
            }
            /*for (MarketCategory marketCategory : categoryList1) {
                //value:label → List
                //向value:label → List放入数据
            }*/

            categoryListDTO.setChildren(childrenList);
            categoryListDTO.setLabel(category.getName());
            categoryListDTO.setValue(category.getId());
            list.add(categoryListDTO);

        }

        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
        catAndBrandVO.setBrandList(brandVoArrayList);
        catAndBrandVO.setCategoryList(list);
        return catAndBrandVO;

    }

    @Override
    public Detail detail(String id) {
        Detail detail = new Detail();
        //Goods
       /* MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(Integer.valueOf(id));*/
        MarketGoods goods = goodsMapper.selectByPrimaryKey(Integer.valueOf(id));
        detail.setGoods(goods);
        //products
        MarketGoodsProductExample example1 = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andGoodsIdEqualTo(Integer.valueOf(id));
        List<MarketGoodsProduct> productList = productMapper.selectByExample(example1);
        detail.setProducts(productList);
        //specification
        MarketGoodsSpecificationExample example2 = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andGoodsIdEqualTo(Integer.valueOf(id));
        List<MarketGoodsSpecification> specificationList = specificationMapper.selectByExample(example2);
        detail.setSpecifications(specificationList);
        //attribute
        MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(Integer.valueOf(id));
        List<MarketGoodsAttribute> attributeList = attributeMapper.selectByExample(example);
        detail.setAttributes(attributeList);
        //categoryIds
        Integer id1 = goods.getCategoryId();
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id1);
        Integer pid=0;
        if (marketCategory!=null){
            LinkedList<Integer> categoryIdsList = new LinkedList<>();
            categoryIdsList.add(pid);
            categoryIdsList.add(id1);
            detail.setCategoryIds(categoryIdsList);
            return detail;
        }else {
            LinkedList<Integer> categoryIdsList = new LinkedList<>();
            categoryIdsList.add(id1);
            detail.setCategoryIds(categoryIdsList);
            return detail;
        }


    }

    @Override
    public void delete(MarketGoods good) {
        Integer id = good.getId();
        Boolean deleted = good.getDeleted();
        MarketGoods newGood = new MarketGoods();
        newGood.setId(id);
        newGood.setDeleted(!deleted);
        goodsMapper.updateByPrimaryKeySelective(newGood);


    }

    @Override
    public void update(UpdatePO update) {
        //good更新

        if (!(update.getGoods()==null)){
            //goodsMapper.updateByExampleSelective(update.getGoods());
              goodsMapper.updateByPrimaryKeySelective(update.getGoods()) ;

        }
        List<MarketGoodsProduct> products = update.getProducts();
        for (MarketGoodsProduct product : products) {
            productMapper.updateByPrimaryKeySelective(product);
        }
//        List<MarketGoodsAttribute> attributes = (List<MarketGoodsAttribute>) update.getAttributes();
        List<MarketGoodsAttribute> attributes = update.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            if (attribute.getGoodsId()==null){
                Integer id = update.getGoods().getId();
                attribute.setGoodsId(id);
                attributeMapper.insertSelective(attribute);
            }else {
                attributeMapper.updateByPrimaryKeySelective(attribute);
            }

        }
        List<MarketGoodsSpecification> specifications = update.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specificationMapper.updateByPrimaryKeySelective(specification);
        }


    }


}
